package com.mochat.mochat.service.userOrder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mochat.mochat.common.model.RequestPage;
import com.mochat.mochat.common.util.wm.ApiRespUtils;
import com.mochat.mochat.dao.entity.UserOrderEntity;
import com.mochat.mochat.dao.entity.WorkContactEmployeeEntity;
import com.mochat.mochat.dao.entity.channel.ChannelCodeEntity;
import com.mochat.mochat.dao.mapper.channel.ChannelCodeMapper;
import com.mochat.mochat.dao.mapper.userOrder.UserOrderMapper;
import com.mochat.mochat.model.channel.RespChannelCodeContactVO;
import com.mochat.mochat.service.AccountService;
import com.mochat.mochat.service.channel.IChannelCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class UserOrderServiceImpl extends ServiceImpl<UserOrderMapper, UserOrderEntity> implements IUserOrderService {

    @Override
    public void addUserOrder(UserOrderEntity userOrderEntity) {

    }

    @Override
    public Page<UserOrderEntity> getListByUserId(String userId, RequestPage page) {
        QueryWrapper<UserOrderEntity> queryWrapper = Wrappers.query();
        queryWrapper.eq("external_userid", userId);
        queryWrapper.orderByDesc("id");
        Page<Map<String, Object>> pageQuery = ApiRespUtils.initPage(page);
        Page<Map<String, Object>> mapPage = this.pageMaps(pageQuery, queryWrapper);

        List<UserOrderEntity> voList = new ArrayList<>();
        for (Map<String, Object> record : pageQuery.getRecords()) {
            UserOrderEntity userOrderEntity = new UserOrderEntity();
            userOrderEntity.setOutTradeNo((String)record.get("out_trade_no"));
            userOrderEntity.setOrderDesc((String)record.get("order_desc"));
            userOrderEntity.setTotalFee((BigDecimal) record.get("total_fee"));
            userOrderEntity.setStatus((String)record.get("status"));
            Date parse = (Date)record.get("created_at");
            userOrderEntity.setCreatedAt(parse);
            voList.add(userOrderEntity);
        }
        Page<UserOrderEntity> pageResult = new Page<UserOrderEntity>();
        pageResult.setRecords(voList);
        pageResult.setTotal(pageQuery.getTotal());
        pageResult.setPages(pageQuery.getPages());
        return pageResult;
    }
}
